package com.hzdesk.media.koreabatchmatvector.dao;

import android.content.Context;
import android.util.Log;


import com.hzdesk.media.koreabatchmatvector.bean.Memory;
import com.hzdesk.media.koreabatchmatvector.bean.MemoryDao;
import com.hzdesk.media.koreabatchmatvector.bean.VideoFileDao;

import java.util.List;

/**
 * Created by Administrator on 2018/5/23.
 */

public class MemoryDaoUtils {
    private static final String TAG = MemoryDaoUtils.class.getSimpleName();
    private DaoManager daoManager;

    public MemoryDaoUtils(Context context) {
        daoManager = DaoManager.getInstance();
        daoManager.init(context);
    }

    /**
     * 插入数据，如果表未创建，先创建Memory表
     * @param memory
     * @return
     */
    public boolean insertMemory(Memory memory) {
        boolean flag = false;
        try {
            flag = daoManager.getDaoSession().getMemoryDao().insert(memory) == -1 ? false : true;
           // Log.i(TAG, "insert Memory :" + flag + "-->" + memory.toString());
        } catch (Exception e) {

        }
        return flag;
    }
    /**
     * 修改一条数据
     * @param memory
     * @return
     */
    public boolean updateMemory(Memory memory) {
        boolean flag = false;
        try {
            daoManager.getDaoSession().update(memory);
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
      //  Log.d(TAG,"update Memory :"+flag+"-->"+memory.toString());
        return flag;
    }
    /**
     *查询所有记录
     * @return
     */
    public List<Memory> queryAllMemory(){
        return daoManager.getDaoSession().loadAll(Memory.class);
    }

    public void Close(){
        daoManager.closeConnection();
    }
    /**
     * @desc 查询出TotalSize
     * */

    public Memory  queryTotalSize(String TotalSize){
        MemoryDao dao =   daoManager.getDaoSession().getMemoryDao();
        Memory memory =  dao.queryBuilder().where(MemoryDao.Properties.StorageType.eq(TotalSize)).unique();
        return memory;

    }
    /**
     * @desc 查询出 UsedSize
     * */
    public Memory queryUserSize(String UsedSize){
        MemoryDao dao =   daoManager.getDaoSession().getMemoryDao();
        Memory memory =  dao.queryBuilder().where(MemoryDao.Properties.StorageType.eq(UsedSize)).unique();
        return memory;
    }
    /**
     * @desc 删除 UserSize
     * */
    public boolean  clear(){
        boolean flag = false ;
        MemoryDao dao =   daoManager.getDaoSession().getMemoryDao();
        List<Memory> mlist =  dao.loadAll();
        for (Memory memory : mlist) {
            if(memory.getStorageType().equals("UsedSize")){
                dao.delete(memory);
                flag = true;
                return flag;
            }
        }
        return flag;
    }
    /**
     * @desc 插入数据
     * */
    public boolean  insertUserSize(Memory memory){
        boolean isclear =    clear();
        if(isclear){
            boolean flag =  insertMemory(memory);
            return flag;
        }
        return isclear;
    }
}
